package com.syh.jn.scm.domain.supplier.entity.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.secret.PasswordUtils;
import cn.redscarf.commons.spring.SpringContextUtils;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.supplier.service.supplierAccountOfPhone.SupplierAccountOfPhoneDomainService;
import com.syh.jn.scm.dto.supplier.system.supplierAccountOfPhone.SupplierAccountOfPhoneSto;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.service.api.supplier.support.enums.SupplierEnums;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * Created by soci.song on 2018/12/13.
 * 服务商手机登录账号管理
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class SupplierAccountOfPhone extends AbstractSimpleAggregate<Long> {
  /**
   * 登录手机账号
   */
  private String loginPhone;
  /**
   * 登录密码
   */
  private String loginPassword;
  /**
   * 姓名
   */
  private String name;
  /**
   * 使用情况
   */
  private Short status;
  /**
   * 默认登录的关联账号
   */
  private Long defualtAccountId;
  /**
   * 绑定的账号ids
   */
  private List<Long> accountIds;
  /**
   * 绑定的账号
   */
  private List<SupplierAccount> accounts;
  /**
   * 所属终端系统:DOWNSTREAM,OPERATION,SUPPLIER,UPSTREAM
   */
  private String terminalType;

  public SupplierAccountOfPhone(SupplierAccountOfPhoneSto sto) {
    //验证绑定账号正确性
    SupplierAccountOfPhoneDomainService domainService = (SupplierAccountOfPhoneDomainService) SpringContextUtils.getBean(SupplierAccountOfPhoneDomainService.class);
    this.accountIds = domainService.verifyAccountPasswords(sto.getAccounts());
    ExtBeanUtils.copyProperties(sto, this);
    this.initDefault();
  }

  @Override
  protected void initDefault() {
    super.initDefault();
    this.terminalType = SupplierEnums.AccountType.SUPPLIER.getStatus();
    this.status = 1;
    this.defualtAccountId = this.accountIds.get(0);
    this.loginPassword = PasswordUtils.generate(this.loginPassword);
  }

  public void destory() {
    //清除与账号的一对多关系
    SupplierAccountOfPhoneDomainService domainService = (SupplierAccountOfPhoneDomainService) SpringContextUtils.getBean(SupplierAccountOfPhoneDomainService.class);
    domainService.clearCorrelation(this.id);
  }

  public void binding(Long accountId) {
    //清除与账号的一对多关系
    SupplierAccountOfPhoneDomainService domainService = (SupplierAccountOfPhoneDomainService) SpringContextUtils.getBean(SupplierAccountOfPhoneDomainService.class);
    domainService.bindingAccount(this.id, accountId);
  }

  public void untying(Long accountId) {
    if (this.defualtAccountId.equals(accountId))
      throw new BusinessException("请先重新设置一个默认登录账号再解绑该账号！");
    //清除与账号的一对多关系
    SupplierAccountOfPhoneDomainService domainService = (SupplierAccountOfPhoneDomainService) SpringContextUtils.getBean(SupplierAccountOfPhoneDomainService.class);
    domainService.untyingAccount(this.id, accountId);
  }

  public void setDefualtSign(Long accountId) {
    //设置默认登录账号
    this.defualtAccountId = accountId;
  }

  public void enable() {
    this.status = 1;
  }

  public void disable() {
    this.status = -1;
  }

  public boolean checkStatus() {
    boolean alter = true;
    for (SupplierAccount account : this.accounts) {
      if (this.defualtAccountId.equals(account.getId()))
        alter = false;
    }
    if (alter) {
      if (this.accounts.isEmpty()) {
        this.defualtAccountId = null;
        this.status = -1;
      } else {
        this.defualtAccountId = this.accounts.get(0).getId();
      }
    }
    return alter;
  }
}
